/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnSlideBuildAdapter;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.utils.Color;

import java.util.List;

/**
 * SlideSlice
 *
 * @since 2021-03-29
 */
public class SlideSlice extends BaseAbilitySlice {
    private static final String[] SPIN_MUNE = {"显示节次时间", "隐藏节次时间", "修改侧边栏背景",
            "修改节次文本颜色", "修改时间文本颜色", "自定义侧边栏", "取消自定义侧边栏"};

    TimetableView mTimetableView;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_slide);
        initViews();
        showPopmenu();
        intents = intent;
    }

    private void showPopmenu() {
        MaterialSpinner spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        spinner.setOnItemSelectedListener((MaterialSpinner.OnItemSelectedListener<String>) (
                view, position, id, item) -> {
            selectLayout(position);
            spinner.setShowFalse();
        });
        spinner.setItems(SPIN_MUNE);
        spinner.setOnNothingSelectedListener(spinner1 -> spinner1.setShowFalse());
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                showTime();
                break;
            case 1:
                hideTime();
                break;
            case 2:
                modifySlideBgColor(new RgbColor(255, 255, 0));
                break;
            case 3:
                modifyItemTextColor(Color.RED.getValue());
                break;
            case 4:
                modifyItemTimeColor(Color.RED.getValue());
                break;
            case 5:
                customSlideView();
                break;
            case 6:
                cancelCustomSlideView();
                break;
            default:
                break;
        }
    }

    private void initViews() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);

        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 显示时间
     * 设置侧边栏构建监听，TimeSlideAdapter是控件实现的可显示时间的侧边栏
     * 只修改了侧边栏的属性，所以只更新侧边栏即可（性能高），没有必要更新全部（性能低）
     *
     * @see OnSlideBuildAdapter
     */
    protected void showTime() {
        String[] times = new String[]{"8:00", "9:00", "10:10", "11:00", "15:00",
                "16:00", "17:00", "18:00", "19:30", "20:30", "21:30", "22:30"};
        OnSlideBuildAdapter slideAdapter = new OnSlideBuildAdapter();
        slideAdapter.setTimes(times);
        mTimetableView.callback(slideAdapter);
        mTimetableView.updateSlideView();
    }

    /**
     * 隐藏时间
     * 将侧边栏监听置Null后，会默认使用默认的构建方法，即不显示时间
     * 只修改了侧边栏的属性，所以只更新侧边栏即可（性能高），没有必要更新全部（性能低）
     */
    protected void hideTime() {
        mTimetableView.callback((ISchedule.OnSlideBuildListener) null)
                .updateSlideView();
    }

    /**
     * 修改侧边栏背景,默认的使用的是OnSlideBuildAdapter，
     * 所以可以强转类型
     *
     * @param color
     */
    protected void modifySlideBgColor(RgbColor color) {
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setBackground(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 修改侧边栏节次文本的颜色值
     *
     * @param color
     */
    protected void modifyItemTextColor(int color) {
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setTextColor(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 修改侧边栏时间文本的颜色值
     *
     * @param color
     */
    protected void modifyItemTimeColor(int color) {
        String[] times = new String[]{"8:00", "9:00", "10:10", "11:00", "15:00", "16:00",
                "17:00", "18:00", "19:30", "20:30", "21:30", "22:30"};
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setTimes(times)
                .setTimeTextColor(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 自定义侧边栏效果
     * 使用自定义的布局文件实现的文字居顶部的效果（默认居中）
     */
    protected void customSlideView() {
        mTimetableView.callback(
                new OnSlideBuildAdapter() {
                    @Override
                    public Component getView(int pos, LayoutScatter inflater, int itemHeight, int marTop) {
                        /**
                         * 获取View并返回，注意设置marTop值
                         */
                        Component v = inflater.parse(ResourceTable.Layout_item_custom_slide, null, false);
                        Text tv = (Text) v.findComponentById(ResourceTable.Id_item_slide_textview);
                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(
                                ComponentContainer.LayoutConfig.MATCH_PARENT,
                                itemHeight);
                        lp.setMargins(0, marTop, 0, 0);
                        tv.setLayoutConfig(lp);
                        tv.setText((pos + 1) + "");
                        return v;
                    }
                })
                .updateSlideView();
    }

    /**
     * 取消自定义的侧边栏，回到默认状态
     * 只需要将监听器置空即可
     */
    protected void cancelCustomSlideView() {
        mTimetableView.callback((ISchedule.OnSlideBuildListener) null)
                .updateSlideView();
    }
}
